package com.pandora.common.check;

import java.util.Date;

import com.pandora.common.exception.EduException;
import com.pandora.common.util.CommonUtils;
import com.pandora.common.util.HMACSHA1Utils;
import com.pandora.common.util.StringUtils;
import com.pandora.common.util.TimeUtils;
import com.pandora.common.util.VerifyUtils;
import com.pandora.framework.constant.Constants;
import com.pandora.framework.constant.ResponseConstants;
import com.pandora.framework.constant.ServiceConstants;
import com.pandora.framework.util.LogUtils;

/**
 * 接口信息校验工具
 * 
 * @author JingChenglong 2017/12/20 18:12
 *
 */
public class ConfigChecks {

	/**
	 * 校验action
	 * 
	 * @param action
	 * @return
	 */
	public static void checkAction(String action) throws EduException {
		if (StringUtils.isEmpty(action) || !StringUtils.isHalfAngle(action) || StringUtils.isBlank(action)
				|| action.length() > 60) {
			throw new EduException(ResponseConstants.SERVICE_INTERFACE_NULL);
		}
	}

	/**
	 * 校验IP合法性
	 * 
	 * @param ipWhite
	 * @param ip
	 */
	public static void checkIp(String ipWhite, String ip) throws EduException {

		if (StringUtils.isNotEmpty(ipWhite)) {
			ipWhite = Constants.PARAM_SEPARATOR + ipWhite + Constants.PARAM_SEPARATOR;
			ip = Constants.PARAM_SEPARATOR + ip + Constants.PARAM_SEPARATOR;
			if (ipWhite.indexOf(ip) == -1) {
				throw new EduException(ResponseConstants.IP_DENIED);
			}
		}
	}

	/**
	 * 校验接口状态
	 * 
	 * @param status
	 */
	public static void checkStatus(String status) throws EduException {

		if (!ServiceConstants.SERVICE_STATUS_RUNNING.equals(status)) {
			throw new EduException(ResponseConstants.SERVICE_UNDER_MAINTENANCE);
		}
	}

	/**
	 * 超时校验
	 * 
	 * @param reqTime
	 * @param timeout
	 */
	public static void checkTimeOut(String reqTime, String timeout) throws EduException {

		if (0 == StringUtils.nullToIntZero(timeout)) {
			return;
		}

		if (StringUtils.isEmpty(reqTime)) {
			throw new EduException(ResponseConstants.REQTIME_CANNOT_BENULL);
		}

		if (!VerifyUtils.checkTime(reqTime)) {
			throw new EduException(ResponseConstants.REQTIME_FORMAT_ERROR);
		}

		Date requestTime = TimeUtils.format(reqTime, TimeUtils.yyyyMMddHHmmssNoCut.toPattern());

		if (requestTime == null) {
			throw new EduException(ResponseConstants.REQTIME_FORMAT_ERROR);
		}

		int timeOutSecond = StringUtils.nullToIntZero(timeout);

		if (timeOutSecond > 0) {
			if (TimeUtils.getSecond(-timeOutSecond).after(requestTime)
					|| TimeUtils.getSecond(timeOutSecond).before(requestTime)) {
				throw new EduException(ResponseConstants.REQTIME_INVALID);
			}
		}
	}

	/**
	 * 校验接口服务类型
	 * 
	 * @param serviceTypeSupport
	 * @param requestServiceType
	 */
	public static void checkServiceType(String serviceTypeSupport, String requestServiceType) throws EduException {

		if (StringUtils.isNotEmpty(serviceTypeSupport)) {
			serviceTypeSupport = Constants.PARAM_SEPARATOR + serviceTypeSupport + Constants.PARAM_SEPARATOR;
			requestServiceType = Constants.PARAM_SEPARATOR + requestServiceType + Constants.PARAM_SEPARATOR;
			if (serviceTypeSupport.indexOf(requestServiceType) == -1) {
				throw new EduException(ResponseConstants.SERVICE_REFUSE);
			}
		}
	}

	/**
	 * 校验请求体MD5格式签名
	 * 
	 * @param reqcontentmd5
	 * @param sign
	 */
	public static void checkSignMD5(String reqcontentmd5, String sign) throws EduException {

		if (StringUtils.isEmpty(sign) || !sign.equalsIgnoreCase(reqcontentmd5)) {
			throw new EduException(ResponseConstants.SIGN_UNMATCH);
		}
	}

	/**
	 * 校验请求体HMACSHA1格式签名
	 * 
	 * @param reqcontentmd5
	 * @param sign
	 */
	public static void checkSignHMACSHA1(String key, String data, String sign) throws EduException {

		try {
			String signMatch = HMACSHA1Utils.HMACSHA1(data, key, Constants.ENCODEING_DEFAULT);
			if (!sign.equalsIgnoreCase(signMatch)) {
				throw new EduException(ResponseConstants.SIGN_UNMATCH);
			}
		} catch (Exception e) {
			LogUtils.logError(CommonUtils.getTraceInfo() + e.getMessage());
			throw new EduException(ResponseConstants.SIGN_UNMATCH);
		}
	}

	/**
	 * 校验通行证
	 * 
	 * @param accessId
	 */
	public static void checkAccessId(String accessId) throws EduException {

		if (StringUtils.isEmpty(accessId)) {
			throw new EduException(ResponseConstants.ACCESSID_ISNULL);
		}
		if (!VerifyUtils.checkMD5(accessId)) {
			throw new EduException(ResponseConstants.ACCESSID_ERROR);
		}
	}
}